﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Code;
using NFine.Domain.Entity.Biz;
using NFine.Domain.Entity.ProductManage;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.SystemManage;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain._04_IRepository.Biz;
using NFine.Repository.Biz;
using NFine.Repository.SystemManage;

namespace NFine.Application.Biz
{
    public class MemberBonusApp
    {

        private IMemberBonusRepository memberBonusRepository = new MemberBonusRepository();
        private IUserRepository userRepository = new UserRepository();
        private IBalanceRecordRepository balanceRecordRepository = new BalanceRecordRepository();

        private UserOrderApp userOrderApp = new UserOrderApp();

        public List<MemberBonusEntity> GetListForBonus(Pagination pagination,  string memberId, int BonusType, string start, string end)
        {
            var expression = ExtLinq.True<MemberBonusEntity>();
            DateTime date = DateTime.Now;

            if (BonusType != -1)
            {
                expression = expression.And(a => a.F_BonusType == BonusType);
            }
            else {
                expression = expression.And(a => a.F_BonusType < 10);
            }
            if (!string.IsNullOrEmpty(memberId))
            {
                expression = expression.And(a => a.F_MemberId == memberId);
            }
            if (!string.IsNullOrEmpty(start))
            {
                date = Convert.ToDateTime(start);
                expression = expression.And(a => a.F_BonusTime >= date);
            }
            if (!string.IsNullOrEmpty(end))
            {
                date = Convert.ToDateTime(end);
                expression = expression.And(a => a.F_BonusTime <= date);
            }
            List<MemberBonusEntity> list = memberBonusRepository.FindList(expression, pagination).OrderByDescending(a => a.F_BonusTime).ToList();
            foreach (var temp in list)
            {
                // 分红人信息
                UserEntity userEntity = userRepository.FindEntity(temp.F_MemberId);
                if (userEntity != null)
                {
                    temp.Account = userEntity.F_Account;
                    temp.RealName = userEntity.F_RealName;
                    temp.NickName = userEntity.F_NickName;
                }
                else
                {
                    temp.Account = "";
                    temp.RealName = "";
                    temp.NickName = "";
                }
                // 消费人信息
                if (string.IsNullOrEmpty(temp.F_OrderId))
                {
                    temp.F_OrderId = "--";
                    temp.OrderMemberName = "--";
                }
                else
                {
                    UserEntity userEntity1 = userOrderApp.GetUserByOrderCode(temp.F_OrderId);
                    if (userEntity1 != null)
                    {
                        temp.OrderMemberName = userEntity1.F_RealName;
                    }
                    else
                    {
                        temp.OrderMemberName = "";
                    }
                }
                

            }

            return list;
        }


        public List<MemberBonusEntity> GetListByBalanceId(Pagination pagination, string Id)
        {
            BalanceRecordEntity balanceRecordEntity = balanceRecordRepository.FindEntity(Id);

            var expression = ExtLinq.True<MemberBonusEntity>();
            expression = expression.And(a => a.F_BonusTime == balanceRecordEntity.F_BalanceTime);
            expression = expression.And(a => a.F_BonusType == 1 || a.F_BonusType == 3);
            List<MemberBonusEntity> list = memberBonusRepository.FindList(expression, pagination);//.OrderByDescending(a => a.F_BonusTime).ToList();

            foreach (var temp in list)
            {
                UserEntity userEntity = userRepository.FindEntity(temp.F_MemberId);
                if (userEntity != null)
                {
                    temp.Account = userEntity.F_Account;
                    temp.RealName = userEntity.F_RealName;
                    temp.MobilePhone = userEntity.F_MobilePhone;
                    temp.CreatorTime = (DateTime)userEntity.F_CreatorTime;
                }
                else
                {
                    temp.Account = "";
                    temp.RealName = "";
                    temp.MobilePhone = ""; temp.CreatorTime = DateTime.Now;
                }
            }
            list = list.OrderBy(a => a.CreatorTime).ToList();
            return list;
        }
        
        /*public List<MemberBonusEntity> GetListByBalanceId(Pagination pagination,string account,string realName,string mobilePhone, string Id)
        {
            BalanceRecordEntity balanceRecordEntity = balanceRecordRepository.FindEntity(Id);

            string sql = "SELECT a.* from biz_member_bonus a WHERE a.F_BonusType = 1";
            sql += " and a.F_BonusTime = '" + balanceRecordEntity.F_BalanceTime.ToString("yyyy-MM-dd HH:mm:ss") + "'";
            sql += " AND a.F_MemberId in(SELECT b.F_Id FROM sys_user b WHERE 1=1";

            if (!string.IsNullOrEmpty(account))
            {
                sql += " and b.F_Account like '%" + account + "%'";
            }
            if (!string.IsNullOrEmpty(realName))
            {
                sql += " and b.F_RealName like '%" + realName + "%'";
            }
            if (!string.IsNullOrEmpty(mobilePhone))
            {
                sql += " and b.F_MobilePhone like '%" + mobilePhone + "%'";
            }

            sql += ")";


        }*/

    }
}
